﻿--[[
    @brief    talk wnd model
    @author   rickonzhang
    @version  1.0
    @date     2015/12/23
--]]
talk_dialog_model = {

    group_pk_result_tbl = {};
    MAX_HISTORY_COUNT = 30;

    get_all_unread_pk_result = function()
        local unread_result_tbl = {};
        for k, group_pk_result_list in pairs(talk_dialog_model.group_pk_result_tbl) do
            for i, item in pairs(group_pk_result_list) do
                table.insert(unread_result_tbl,base_utility.table_to_json(item));
            end
        end
        return unread_result_tbl;
    end;

    get_unread_pk_result_count = function(group_id)
        local count  = 0;
        if talk_dialog_model.group_pk_result_tbl[group_id] ~= nil then
            count = #talk_dialog_model.group_pk_result_tbl[group_id];
        end
        return count;
    end;

    get_unread_pk_result_list = function(group_id)
        return talk_dialog_model.group_pk_result_tbl[group_id];
    end;

    --一旦显示过后，就不需要保存了，调用该接口clear掉
    clear_unread_pk_result_list = function(group_id)
        if talk_dialog_model.group_pk_result_tbl[group_id] ~= nil then
            talk_dialog_model.group_pk_result_tbl[group_id] = nil;
        end
    end;

    --从文件读取出来的，保存到缓存中
    save_history_pk_detail = function(pk_result_list)
        if next(pk_result_list) then
            for key, var in ipairs(pk_result_list) do
                local group_id = var["group_id"];
                if talk_dialog_model.group_pk_result_tbl[group_id] == nil then                     
                    talk_dialog_model.group_pk_result_tbl[group_id] = {};
                end
                if #talk_dialog_model.group_pk_result_tbl[group_id] < talk_dialog_model.MAX_HISTORY_COUNT then
                    table.insert(talk_dialog_model.group_pk_result_tbl[group_id], var);
                else
                    base_utility.log("[talk_dialog_model] histroy is full:"..tostring(group_id), true);
                end
            end
            -- 做排序，时间早的放前面
            for key, var in pairs(talk_dialog_model.group_pk_result_tbl) do                   
                table.sort(talk_dialog_model.group_pk_result_tbl[key], function(a, b)
                                                            return a.time > b.time;
                                                       end
                      );
            end
            
        end
    end;

    save_group_pk_detail = function(group_id, result_tbl)
        if talk_dialog_model.group_pk_result_tbl[group_id] == nil then
            talk_dialog_model.group_pk_result_tbl[group_id] = {};
        end
        -- 最多保存30条记录
        if #talk_dialog_model.group_pk_result_tbl[group_id] == talk_dialog_model.MAX_HISTORY_COUNT then
            table.remove(talk_dialog_model.group_pk_result_tbl[group_id], talk_dialog_model.MAX_HISTORY_COUNT);
            base_utility.log("[talk_dialog_model] save_group_pk_detail is full:"..tostring(group_id), true);
        end
        -- 直接插在最前面
        table.insert(talk_dialog_model.group_pk_result_tbl[group_id], 1, result_tbl);
    end; 
}